package cn.gaotu.org.controller;

import cn.gaotu.basic.util.AjaxResult;
import cn.gaotu.basic.util.PageList;
import cn.gaotu.org.domain.Employee;
import cn.gaotu.org.query.EmployeeQuery;
import cn.gaotu.org.service.EmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ZouX
 * @date 2022/8/14 10:36
 */
@RestController
@RequestMapping("/employee")
@Api(value = "部门的api",description = "部门相关的CRUD")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    // http://localhost:8080/employee/1 Get
    @ApiOperation(value = "通过ID查询")
    @GetMapping("/{id}")
    public Employee findById(@PathVariable("id")Long id){
        return employeeService.findById(id);
    }

    // http://localhost:8080/employee Get
    @GetMapping
    @ApiOperation(value = "查询所有")
    public List<Employee> findAll(){
        return employeeService.findAll();
    }

    // http://localhost:8080/employee/1 Delete
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除")
    public AjaxResult remove(@PathVariable("id")Long id){
        try {
            employeeService.DeleteDep(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "删除失败");
        }
    }
    // http://localhost:8080/employee Put 参数：{} - 请求体
    @PutMapping
    //@RequestBody Employee employee - 从请求体中获取数据赋值给Employee对象
    //这个是添加或者更新
    @ApiOperation(value = "更新或者保存")
    public AjaxResult addOrUpdate(@RequestBody Employee employee) {
        System.out.println(employee);
        try {
            if (employee.getId() == null) {
                employeeService.save(employee);
            } else {
                employeeService.update(employee);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "操作失败");
        }
    }
    // http://localhost:8080/ Poemployeest 参数：{} - 请求体
    //  查询分页
    @PostMapping
    @ApiOperation(value = "查询页面")
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery employeeQuery) {
        return employeeService.queryPage(employeeQuery);
    }
    //批量删除
    @PatchMapping
    @ApiOperation(value = "根据id批量删除")
    public AjaxResult patchRemove(@RequestBody List<Long> ids){
        try {
            employeeService.patchRemove(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "批量删除失败");
        }
    }

}
